﻿using System.Collections.Generic;

namespace OverStore.Runtime.ObjectRepository
{
    /// <summary>
    /// Defines equalty of two instances based on reference equality.
    /// </summary>
    /// <typeparam name="T">OverrideDbType of instance to compare.</typeparam>
    /// <remarks>
    /// OverrideDbType uses implementation of <see cref="M:System.Object.GetHashCode"/> provided by 
    /// <typeparamref name="T"/> type.
    /// </remarks>
    public sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T>
        where T : class
    {
        /// <summary>
        /// Singleton instance of <see cref="ReferenceEqualityComparer{T}"/>.
        /// </summary>
        public static readonly IEqualityComparer<T> Instance =
            new ReferenceEqualityComparer<T>();

        private ReferenceEqualityComparer()
        {
        }

        /// <summary>
        /// Determines whether the specified objects are equal.
        /// </summary>
        /// <param name="x">The first object of type <paramref name="T"/> to compare.</param>
        /// <param name="y">The second object of type <paramref name="T"/> to compare.</param>
        /// <returns>
        /// true if the specified objects are equal; otherwise, false.
        /// </returns>
        public bool Equals(T x, T y)
        {
            return ReferenceEquals(x, y);
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        /// The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.
        /// </exception>
        public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }
    }
}
